<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Clustered Application</title>
    <link rel="stylesheet" href="/assets/lib/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/assets/css/default.css">
  </head>
  <body>
    <h1>demo for run-js</h1>
    <div id="main"></div>
    <script src="/assets/lib/require/require.min.js"></script>
    <script>
      
      var r = require;
      //如果用户要放到本地域，需要改几个url
      r(['/assets/js/require.config.js'], function(config) {
        r.config(config);

        r(['refresh']);
        r(['lodash'], function(_) { window.__ = _; })
        r(['hy5b/log/index', '../abc/js/env'], function(log, env) {
          window.env = env;
          window.$env = {};

          require(['backbone', 'underscore', 'jsonview', 'jquery'], App);

          require(['baqq']);

          define('baqq', ['jquery'], function() {
            //$.deparam
            require(['/assets/lib/jquery/jquery.ba-bbq.js'])
          })
        });

      });
      
      function App(Backbone, _, jsonview, $) {
        require(['js/app'], function(app) {
          app();
        });
      }

    </script>
    <script>
      define('js/app', function(require, exports, module) {
        var Backbone = require('backbone');
        var $ = require('jquery');
        var _ = require('underscore');
        var jsonview = require('jsonview');

        function main() {
          var $app = $('<div id="app">');
          $('#main').html($app);

          switch (1) {
            case 1:
              SocketIoClientInstance();
              break;
            case 2:
              NativeWebsocketClientInstance()
              break;
            // case 3:
            //   NativeWebsocketClientInstance()
            //   break;
          }
        }

        function NativeWebsocketClientInstance() {

          // var url = 'wss://show.uyuntest.cn/api/ws/dataset';
          var websocketClient = new WebSocket('ws://localhost:10000/api/ws/dataset');
          // Connection opened
          websocketClient.addEventListener('open', function (event) {
              console.log('socket open: ', event)
              websocketClient.send('Hello Server!');
          });
          // Listen for messages
          websocketClient.addEventListener('message', function (event) {
              console.log('socket receive message: ', event.data);
          });

          websocketClient.onclose = function(event) {
            console.log('socket close: ', event);
          }

          websocketClient.onerror = function(event) {
            console.log('socket error: ', event);
          }
        }

        function SocketIoClientInstance() {

          var io = require('io');

          var websocketClient = io.connect('ws://localhost:10000', {path: '/api/ws/dataset'});
          websocketClient.on('push', function (data) {
            console.log(data);
            websocketClient.emit('push_ack', { status: 0 });
          });
        }

        return main;
      })
    </script>
  </body>
</html>